<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/18
 * Time: 14:45
 */

namespace app\common\model;
use think\Model;

class PeccData extends Model
{
    protected $name = "pecc_data";

    /**
     * 添加用户违章信息
     * @param $data
     * @return int|string
     */
    public function addUserPecInfo($data) {

        $res = $this->insert($data);

        return $res;
    }

    /**
     * 更新用户违章信息
     * @param $where
     * @param $data
     * @return PeccData
     */
    public function updateUserPecInfo ($where, $data) {

        $res = $this->where($where)->update($data);

        return $res;
    }

    /**
     * 获取用户违章信息
     * @param $where
     * @param $field
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserPecInfoList ($where, $filed, $limit, $request, $keyword) {

        if ($keyword) {
            $data = $this->alias('pl')
                ->join('car_auth ca', 'pl.user_id = ca.user_id', 'left')
                ->field($filed)
                ->where($where)
//                ->whereOr('pl.user_id', $keyword)
//                ->whereOr('ca.name', $keyword)
//                ->whereOr('pl.id', $keyword)
//                ->whereOr('ca.phone', $keyword)
                ->whereOr('pl.user_id|ca.name|pl.id|ua.phone','like',"%$keyword%")
                ->order('pl.create_time desc')
                ->paginate($limit, false, ['query' => $request->get()]);
        } else {
            $data = $this->alias('pl')
                ->join('car_auth ca', 'pl.user_id = ca.user_id', 'left')
                ->field($filed)
                ->where($where)
                ->order('pl.create_time desc')
                ->paginate($limit, false, ['query' => $request->get()]);
        }

        return $data;
    }


    public function getUserPecDataDetail($where, $filed){

        $detail = $this
            ->alias('pl')
            ->join('car_auth ca', 'pl.user_id = ca.user_id', 'left')
            ->join('user_auth ua', 'pl.user_id = ua.user_id', 'left')
            ->field($filed)
            ->where($where)
            ->field($filed)
            ->find();

        return $detail;
    }
}